package com.example.gybicycle.common.util;

import com.example.gybicycle.common.security.AuthFilter;
import com.example.gybicycle.entity.LoginInfo;
import com.example.gybicycle.common.redis.RedisUtil;
import com.example.gybicycle.entity.wx.WXLoginInfo;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LoginUtil {
    public static  LoginUtil loginUtil;
    public static final int login_timeout=60*60*2;
    @Autowired
    RedisUtil redisUtil;
    @PostConstruct
    public void init(){
        loginUtil=this;
        loginUtil.redisUtil=this.redisUtil;
    }
    public  static void setLoginInfo(LoginInfo info){
        loginUtil.redisUtil.set("loginInfo:"+info.getAuthorization(),info,login_timeout);
    }



    public  static void setCode(LoginInfo info){
        loginUtil.redisUtil.set("loginInfo:"+info.getAuthorization(),info,60*10);
    }
    public  static LoginInfo getLoginInfo(String token){
        LoginInfo info=(LoginInfo)loginUtil.redisUtil.get("loginInfo:"+token);
        loginUtil.redisUtil.expire("loginInfo:"+token,login_timeout);
        return info;
    }
    public  static LoginInfo getLoginInfo(HttpServletRequest request){
        String token = AuthFilter.findToken(request);
        if(token==null)
            return null;
            System.out.println("login-token-show:"+token);
        LoginInfo info=getLoginInfo(token);
        return info;
    }
    public  static void setWxLoginInfo(WXLoginInfo info){
        loginUtil.redisUtil.set("wx-loginInfo:"+info.getAuthorization(),info,login_timeout);
    }
    public  static WXLoginInfo getWxLoginInfo(HttpServletRequest request){
        String token = AuthFilter.findToken(request);
        if(token==null)
            return null;
        WXLoginInfo info=getWxLoginInfo(token);
        return info;

    }
    public  static WXLoginInfo getWxLoginInfo(String token){
        WXLoginInfo info=(WXLoginInfo)loginUtil.redisUtil.get("wx-loginInfo:"+token);
        loginUtil.redisUtil.expire("wx-loginInfo:"+token,login_timeout);
        return info;
    }
    public  static void wxloginout(HttpServletRequest request){
        String token = AuthFilter.findToken(request);
        LoginInfo info=(LoginInfo)loginUtil.redisUtil.hget("wx-loginInfo:",token);
        if(info!=null)
            loginUtil.redisUtil.hdel("wx-loginInfo:",token);
    }
    public  static void delLoginInfo(String token){
        loginUtil.redisUtil.hdel("loginInfo",token);

    }
    public  static void loginout(HttpServletRequest request){
        String token = AuthFilter.findToken(request);
        LoginInfo info=(LoginInfo)loginUtil.redisUtil.hget("loginInfo",token);
        if(info!=null)
            loginUtil.redisUtil.hdel("loginInfo",token);
    }

    public static boolean isLogin(HttpServletRequest request){
        String token = AuthFilter.findToken(request);
        LoginInfo user =getLoginInfo(token);
        if(user!=null)
            return true;
        else  return  false;
    }
    public static boolean isLogin(String token) {
        LoginInfo user =getLoginInfo(token);
        if(user!=null)
            return true;
        else  return  false;
    }
}
